package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.domain.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.repository.*;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService{

    @Autowired
    private StrategyRepository strategyRepository;
    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;
    @Autowired
    private StrategyThemeRepository strategyThemeRepository;
    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private DestinationServiceImpl destinationService;
    @Autowired
    private TravelRepository travelRepository;


    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        StrategyCatalog strategyCatalog = strategyCatalogRepository.findById(strategy.getCatalogId()).get();
        StrategyTheme strategyTheme = strategyThemeRepository.findById(strategy.getThemeId()).get();
        strategy.setDestId(strategyCatalog.getDestId());
        strategy.setDestName(strategyCatalog.getDestName());
        strategy.setCatalogName(strategyCatalog.getName());
        strategy.setThemeName(strategyTheme.getName());
        strategy.setCreateTime(new Date());
        //是否是国外
        List<Destination> list = new ArrayList<>();
        destinationService.getToastList(list,strategyCatalog.getDestId());
        Collections.reverse(list);
        if(list != null && list.size()>0){
            Destination destination = list.get(0);
            if("中国".equals(destination.getName())){
                strategy.setIsabroad(Strategy.ABROAD_NO);
            }else{
                strategy.setIsabroad(Strategy.ABROAD_YES);
            }
        }
        strategyRepository.save(strategy);
    }

    @Override
    public void delete(String id) {
        strategyRepository.deleteById(id);
    }

    @Override
    public void update(Strategy strategy) {
        //这是可能修改的数据
        StrategyCatalog strategyCatalog = strategyCatalogRepository.findById(strategy.getCatalogId()).get();
        StrategyTheme strategyTheme = strategyThemeRepository.findById(strategy.getThemeId()).get();
        strategy.setDestId(strategyCatalog.getDestId());
        strategy.setDestName(strategyCatalog.getDestName());
        strategy.setCatalogName(strategyCatalog.getName());
        strategy.setThemeName(strategyTheme.getName());

        List<Destination> list = new ArrayList<>();
        destinationService.getToastList(list,strategyCatalog.getDestId());
        Collections.reverse(list);
        if(list != null && list.size()>0){
            Destination destination = list.get(0);
            if("中国".equals(destination.getName())){
                strategy.setIsabroad(Strategy.ABROAD_NO);
            }else{
                strategy.setIsabroad(Strategy.ABROAD_YES);
            }
        }
        //这是需要拿到的数据,不能自己设置
        Strategy strategy1 = this.get(strategy.getId());
        strategy.setCreateTime(strategy1.getCreateTime());
        strategy.setViewnum(strategy1.getViewnum());
        strategy.setReplynum(strategy1.getReplynum());
        strategy.setFavornum(strategy1.getFavornum());
        strategy.setSharenum(strategy1.getSharenum());
        strategy.setThumbsupnum(strategy1.getThumbsupnum());
        strategyRepository.save(strategy);
    }

    @Override
    public Strategy get(String id) {
        Strategy strategy = strategyRepository.findById(id).orElse(null);
        return strategy;
    }

    @Override
    public List<Strategy> list() {

        return strategyRepository.findAll();
    }

    @Override
    public Page<Strategy> queryPageList(StrategyQuery qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        return DBHelper.query(template,Strategy.class,query,pageable);
    }

    /**
     * 下架操作
     * @param state
     * @param id
     */
    @Override
    public void changeState(int state, String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        Update update = new Update();
        update.set("state",state);
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> strategiesViewnumTop3(String destId) {
        //根据目的地查攻略
        //选取前三个攻略
        Pageable pageable = PageRequest.of(0,3, Sort.Direction.DESC,"viewnum");
        List<Strategy> strategies = strategyRepository.findByDestId(pageable,destId);
        return strategies;
    }

    @Override
    public List<Travel> travelsViewnumTop3(String destId) {
        Pageable pageable = PageRequest.of(0,3, Sort.Direction.DESC,"viewnum");
        List<Travel> Travels = travelRepository.findByDestId(pageable,destId);
        return Travels;
    }

    @Override
    public void updateStragegyList(StrategyStatisVO vo) {
       /*由于攻略的更新操作对一些无法拿到的数据处理方式是从数据库
        中读取的所以这里使用update方法存在问题
        Strategy strategy = this.get(vo.getStrategyId());
        strategy.setViewnum(vo.getViewnum());
        strategy.setThumbsupnum(vo.getThumbsupnum());
        strategy.setSharenum(vo.getSharenum());
        strategy.setFavornum(vo.getFavornum());
        strategy.setReplynum(vo.getReplynum());
        this.update(strategy);*/

        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));
        Update update = new Update();
        update.set("viewnum",vo.getViewnum());
        update.set("replynum",vo.getReplynum());
        update.set("favornum",vo.getFavornum());
        update.set("sharenum",vo.getSharenum());
        update.set("thumbsupnum",vo.getThumbsupnum());
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> queryByDestName(String destName) {
        List<Strategy> strategyList = strategyRepository.findByDestName(destName);

        return strategyList;
    }

}
